package cn.edu.sasu.service.Impl;

import cn.edu.sasu.common.base.LayuiRespResult;
import cn.edu.sasu.common.base.RespResult;
import cn.edu.sasu.common.constants.Constant;

import cn.edu.sasu.domain.dto.SysDeptDTO;
import cn.edu.sasu.domain.entity.SysDept;
import cn.edu.sasu.domain.entity.SysDoctor;
import cn.edu.sasu.domain.entity.SysHospital;
import cn.edu.sasu.domain.entity.UpDept;
import cn.edu.sasu.domain.vo.SysDeptVO;
import cn.edu.sasu.mapper.SysDeptMapper;
import cn.edu.sasu.mapper.SysDoctorMapper;
import cn.edu.sasu.service.SysDeptService;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.StrUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 2024/4/27
 *
 * @author ganxue
 */
@Service
public class SysDeptServiceImp implements SysDeptService {
    @Resource
    private SysDeptMapper sysDeptMapper;
    @Resource
    private SysDoctorMapper sysDoctorMapper;

    @Override
    public LayuiRespResult deptList(Integer page, Integer limit,Integer hospitalId) {
        Integer offset = (page - 1) * limit;
        Integer rows = limit;
        List<SysDept> sysDepts = sysDeptMapper.deptList(Constant.DELETE_FLAG_YES,offset,rows,hospitalId);

        Integer totalSize = sysDeptMapper.countDepts(Constant.DELETE_FLAG_YES,hospitalId);
        return LayuiRespResult.success(totalSize,sysDepts);
    }

    @Override
    public List<SysDeptVO> getDepts(Integer deleteFlag,Integer hospitalId) {
        return sysDeptMapper.getAllDept(deleteFlag,hospitalId);
    }



    @Override
    public LayuiRespResult search(Integer page, Integer limit, String deptName) {
        Integer offset = (page - 1) * limit;
        Integer rows = limit;
        List<SysHospital> sysHospitals = sysDeptMapper.searchSysDoctorList(Constant.DELETE_FLAG_YES,offset,rows,deptName);
        Integer totalSize = sysDeptMapper.countSearchSysDept(Constant.DELETE_FLAG_YES,deptName);
        return LayuiRespResult.success(totalSize,sysHospitals);
    }

    @Override
    public RespResult saves(SysDeptDTO sysDeptDTO) {
        SysDept dept = sysDeptMapper.findSysDeptByDoctorName(sysDeptDTO.getDeptId());
        if(dept != null){
            return RespResult.error("该用户名已经存在，请重新输入！");
        }
        // 对象拷贝
        SysDept sysDept = BeanUtil.copyProperties(sysDeptDTO,SysDept.class);
        //获取当前的时间
        DateTime now = DateTime.now();
        sysDept.setCreateDate(now);
        sysDeptMapper.save(sysDept);
        return RespResult.success("添加成功");
    }

    @Override
    public List<UpDept> getDeptsList(Integer deleteFlagYes) {
        List<UpDept> upDepts = sysDeptMapper.getDeptsList(deleteFlagYes);
        return upDepts;
    }

    @Override
    public RespResult updates(SysDeptDTO sysDeptDTO) {
        SysDept sysDept = BeanUtil.copyProperties(sysDeptDTO,SysDept.class);

        sysDeptMapper.update(sysDept);
        return  RespResult.success("修改成功");

    }

}
